<template>
  <div class="allsearch">
    <section class="search-content">
      <el-row :gutter="4" type="flex" justify="start">
        <el-col :span="24">
          <el-input v-model="username" style="width:200px;" placeholder="用户名" @keyup.enter.native="handleFilter"/>
          <el-button type="primary" size="medium" @click="handleFilter">搜索</el-button>
          <el-button size="medium" type="info" @click="clearParams">清空</el-button>
          <el-button type="warning" size="medium">导出</el-button>
        </el-col>
      </el-row>
    </section>
    <subtable
      :table-label="inviteLabel"
      :table-data="listFilter.slice((currentpage-1)*pagesize,currentpage*pagesize)"
    />
    <pagination
      :total="total"
      :current-page="currentpage"
      :page-size="pagesize"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
  </div>
</template>

<script>
import Subtable from '@/components/Eltable'
import Pagination from '@/components/Pagination' // 分页组件
import { getUserRecord } from '@/api/operate/invitation.js' // 数据接口
import { getTenantId } from '@/utils/auth'
export default {
  components: {
    Subtable,
    Pagination
  },
  data() {
    return {
      username: '',
      total: 0,
      currentpage: 1,
      pagesize: 10,
      listFilter: [],
      inviteLabel: [
        {
          label: '用户名', width: '', prop: 'nickName'
        }, {
          label: '邀请人数', width: '', prop: 'invitePersionNum'
        }, {
          label: '付费用户数', width: '', prop: 'paidPersionNum'
        }, {
          label: '订单消费总额', width: '', prop: 'orderTotal'
        }, {
          label: '订单虚拟币总额', width: '', prop: 'coinTotal'
        }, {
          label: '订单现金总额', width: '', prop: 'moneyTotal'
        }
      ],
      inviteData: []
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    handleSizeChange(val) {
      console.log(val)
      this.currentpage = 1
      this.pagesize = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    handleCurrentChange(val) {
      console.log(val)
      this.currentpage = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    handleFilter() {
      this.listFilter = this.inviteData.filter(item => {
        if (this.username && item.nickName.toLowerCase().indexOf(this.username.toLowerCase()) === -1) return false
        return true
      })
      this.total = this.listFilter.length
    },
    clearParams() {
      this.username = ''
      this.getData()
    },
    getData() {
      getUserRecord().then(response => {
        this.inviteData = response.data.items.filter(item => {
          if (item.tenantId !== getTenantId()) return false
          return true
        })
        this.total = this.inviteData.length
        this.listFilter = this.inviteData
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.allsearch {
  margin-top: 0px;
  padding: 0;
  .search-content {
    width: 100%;
  }
}
</style>
